﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using b7.Models;
using System.Configuration;
using System.Text;

namespace b7.Controllers
{
    public class HomeController : Controller
    {
        //
        // GET: /Home/
        private StringBuilder sbMenu = new StringBuilder();
        private b7Models db = new b7Models();
        public ActionResult Index()
        {
            
            var sanpham = db.Sanphams.ToList();
            
           // var sanpham = from d in db.Sanphams
            //              where  
            //              select d;
            //var pageNumber = page ?? 1;//nếu ko có trang được truy vấn, mặc ddunhj là trang dầu tiên(1)
            //var onePageOfProducts = sanpham.ToPageList(pageNumber, 25);
            return View(sanpham);
        }
        public ActionResult chitietsp(int id)
        {
            Sanpham sanpham = db.Sanphams.Find(id);
            return View(sanpham);
        }
        [ChildActionOnly]
        public ActionResult Menu()
        {
            List<Menu> menu = db.Menus.ToList();
            return this.PartialView("Menu",menu);
        }
        public ActionResult SlideNew()
        {
            var baiviet = db.BaiViets.ToList();
            return PartialView("_SlideNew");
            //cau can lam gi?

        }

        //phan quyền LogOn
        [Authorize(Roles = "admin,member")] //Chi co Admin va ThanhVien Moi Dung duoc acction nay
        [HttpGet] //phuong thuc get
        public ActionResult ThongTin()
        {
            ////-------- ket noi den database lay thong tin account --------------
            // LoginAccountEntities db = new LoginAccountEntities();//ket noi voi database
            Member account = db.Members.Single(x => x.UserName.Equals(User.Identity.Name)); //tuong duong voi cau lenh "Select Top 1 From Accounts Where NameAccount = @User.Identity.Name"
            if (account != null)//kiem tra xem tim thay thong tin cua account ko?
            {
                ViewBag.Profile = account; //tao ViewBag.Prodile de luu thong tin account chuyen cho client
            }

            return View("ThongTin");//nap trang thong tin cho client
        }
        [Authorize(Roles = "admin,member")] //Chi co Admin va ThanhVien Moi Dung duoc acction nay
        [HttpGet] //phuong thuc get
        public ActionResult DoiMatKhau()
        {
            return View("DoiMatKhau"); //Nap giao dien trang DoiMatKhau cho client
        }
        [Authorize(Roles = "admin,member")] //Chi co Admin va ThanhVien Moi Dung duoc acction nay
        [HttpPost] //phuong thuc get
        public ActionResult DoiMatKhau(ChangePasswordModel model)
        {
            if (ModelState.IsValid)//kiem tra xem mat khau doi da hop le chua
            {
                ////neu mat khau moi hop le thi ket noi voi database de kiem tra mat khau cu da dung chu
                // LoginAccountEntities db = new LoginAccountEntities();//ket noi voi database
                List<Member> accounts = db.Members.Where(x => x.UserName.Equals(User.Identity.Name) && x.Password.Equals(model.OldPassword)).ToList();//tuong duong voi cau lenh "Select * from Accounts where NameAccount = @User.Identity.Name && Password = model.OldPassword"
                if (accounts.Count > 0)//neu tim thay tai khoan voi dung mat khau cau
                {
                    Member account = db.Members.Single(x => x.UserName.Equals(User.Identity.Name) && x.Password.Equals(model.OldPassword));//tuong duong voi cau lenh "Select Top 1 from Accounts where NameAccount = @User.Identity.Name && Password = model.OldPassword"
                    //trong Entity muon edit du lieu trong database thi can phai tim duoc du lieu can thay doi truoc roi edit thong tin sau do save
                    account.Password = model.NewPassword;//thay doi mat khau moi thay cho mat khau cu
                    db.SaveChanges();// luu lai du lieu thay doi .tuong duong voi cau lenh "Update Accounts Set Password = @model.NewPassword Where NameAccount= @User.Identity.Name and Password = @model.OldPassword"
                    return RedirectToAction("DoiMatKhauThanhCong");//doi mat khau thanh cong xong thi chuyen sang acction: DoiMatKhauThanhCong()
                }
                else
                {
                    ModelState.AddModelError("", "mat khau cu sai");//add them thong bao
                }
            }

            //neu du lieu doi mat khau khong dung cu phap thi nap lai trang doi mat khau va hien thi thong bao loi
            return View("DoiMatKhau", model);//nap lai trang thong tin cho client va chuyen model
        }
        [Authorize(Roles = "admin,member")] //Chi co Admin va ThanhVien Moi Dung duoc acction nay
        [HttpGet] //phuong thuc get
        public ActionResult DoiMatKhauThanhCong()
        {
            return View("DoiMatKhauThanhCong");//nap trang doi mat khau cho client
        }
        [Authorize(Roles = "")] //Chi can dang nhap la dung duoc Action nay
        [HttpGet] //phuong thuc get
        public ActionResult PhanQuyen()
        {
            return View("PhanQuyen");//nap trang doi mat khau cho client
        }
       
        [ChildActionOnly]
        public ActionResult ViewNew()
        { 
            List<BaiViet> baiviet = db.BaiViets.ToList();
            return this.PartialView("_Tinlienquan",baiviet);
        }
        public ActionResult chitiettin(int id)
        { 
            BaiViet baiviet=db.BaiViets.Find(id);
            return View(baiviet);
        }
        [ChildActionOnly]
        public ActionResult ViewSlide()
        {
            List<BaiViet> baiviet = db.BaiViets.ToList();
            return this.PartialView("_SlideNew", baiviet);
        }
        [ChildActionOnly]
        public ActionResult Search(string nhomsp, string searchstring)
        {
            var listnhom = new List<string>();
            var querynhom = from d in db.NhomSanphams
                            orderby d.Name
                            select d.Name;
            listnhom.AddRange(querynhom.Distinct());
            ViewBag.nhomsp = new SelectList(listnhom);
            var sp = from m in db.Sanphams
                     select m;
            if (!String.IsNullOrEmpty(searchstring))
            {
                sp = sp.Where(s => s.Name.Contains(searchstring));
                return View(sp);
            }

            //if (int.IsNullOrEmpty(nhomsp))
            //    return View(sp);
            else
            {
                //sp = sp.Where(x => x.NhomSanpham.Name == nhomsp);

                return View(sp.Where(x => x.Name == nhomsp));
            }            
        }
        [ChildActionOnly]
        public ActionResult splienquan()
        {
            var sanpham = db.Sanphams.ToList();
            return PartialView("_Splienquan",sanpham);
        }
        [ChildActionOnly]
        public ActionResult Anhchitietsp(int id)
        {
            Sanpham sanpham = db.Sanphams.Find(id);
            return PartialView("_Anhchitietsp",sanpham);
        }

        public ActionResult tpmenudoc(int id)
        {
            //NhomSanpham nhomsp = db.NhomSanphams.Find(id);
            //return View(nhomsp);
            var query = from d in db.Sanphams where d.NhomSanpham.NhomSanphamId.Equals(id) orderby d.NgayBatdau select d;
            return View(query);
        }

        [ChildActionOnly]
        public ActionResult Menudoc()
        {
            List<NhomSanpham> nhomsp = db.NhomSanphams.ToList();
            return this.PartialView("_Menudoc", nhomsp);
        }
        //public ActionResult Mypham()
        //{
        //    var query = from d in db.Sanphams where d.NhomSanpham.Name.Contains("Mỹ phẩm") orderby d.NgayBatdau select d;
        //    return View(query);
        //}
        //public ActionResult Batdongsan()
        //{
        //    var query = from d in db.Sanphams where d.NhomSanpham.Name.Contains("Bất động sản") orderby d.NgayBatdau select d;
        //    return View(query);
        //}
        //public ActionResult Amthuc()
        //{
        //    var query = from d in db.Sanphams where d.NhomSanpham.Name.Contains("Ẩm thực") orderby d.NgayBatdau select d;
        //    return View(query);
        //}
        //public ActionResult Dienmay()
        //{
        //    var query = from d in db.Sanphams where d.NhomSanpham.Name.Contains("Điện máy") orderby d.NgayBatdau select d;
        //    return View(query);
        //}
        //public ActionResult Dienthoai()
        //{
        //    var query = from d in db.Sanphams where d.NhomSanpham.Name.Contains("Điện thoại") orderby d.NgayBatdau select d;
        //    return View(query);
        //}
        //public ActionResult Mynghe()
        //{
        //    var query = from d in db.Sanphams where d.NhomSanpham.Name.Contains("Mỹ nghệ") orderby d.NgayBatdau select d;
        //    return View(query);
        //}
        //public ActionResult Noithat()
        //{
        //    var query = from d in db.Sanphams where d.NhomSanpham.Name.Contains("Nội thất") orderby d.NgayBatdau select d;
        //    return View(query);
        //}

        //public ActionResult Amthuc()
        //{
        //    var query = from d in db.Sanphams where d.NhomSanpham.Name.Contains("Ẩm thực") orderby d.NgayBatdau select d;
        //    return View(query);
        //}

        public ActionResult Thoitrangnam()
        {
            var query = from d in db.Sanphams where d.Theloai.Name.Contains("Thời trang nam") orderby d.NgayBatdau select d;
            return View(query);
        }

        public ActionResult Thoitrangnu()
        {
            var query = from d in db.Sanphams where d.Theloai.Name.Contains("Thời trang nữ") orderby d.NgayBatdau select d;
            return View(query);
        }

        public ActionResult dscuahang()
        {
            var ch = from d in db.Members
                     where d.Role.Contains("adminshop")
                     select d;
            return View(ch);
        }
        public ActionResult Cuahang(int id)
        {
            Member member = db.Members.Find(id);
            return View(member);
        }
        public ActionResult sanpham()
        {
            var sp = from d in db.Sanphams
                     select d;
            return View(sp);
        }
        public ActionResult Tangqua()
        {
            var tq = from d in db.Sanphams
                     where d.SalePrice == 0
                     select d;
            return View(tq);
        }
        public ActionResult Tintuc()
        {
            var tt = from d in db.BaiViets
                     select d;
            return View(tt);
        }
        public ActionResult Lienhe()
        {
            return View();
        }
    }
}
